<?php

namespace app\api\controller;

use app\api\controller\Base;
use app\goods\model\ImsEweiShopCategory;
use app\goods\model\ImsEweiShopGoods;
use app\goods\model\ImsEweiShopGoodsSpec;
use think\facade\Request;

class Goods extends Base
{
    public function index()
    {

        $cid = Request::param('cid');
        $kw = Request::param('kw');

        $limit = $this->request->get('limit',10);
        $page = $this->request->get('page',1);
        $lat = $this->request->get('lat',0);
        $lng = $this->request->get('lng',0);
        $ishot = $this->request->get('ishot',0);//热销
        $isnew = $this->request->get('isnew',0);//新品
        $isdiscount = $this->request->get('isdiscount',0);//折扣
        $isrecommand = $this->request->get('isrecommand',0);//推荐
        
        $offset = ($page - 1) * $limit;

        $where  = [
            'status' => 1,
            'uniacid' => 2,
            'parent_merchid' => 0,
            'parent_gid' => 0,
            'checked' => 0,
            'deleted' => 0
        ];
        if ($ishot) {
            $where['ishot'] = $ishot;
        }
        if ($isnew) {
            $where['isnew'] = $isnew;
        }
        if ($isdiscount) {
            $where['isdiscount'] = $isdiscount;
        }
        if ($isrecommand) {
            $where['isrecommand'] = $isrecommand;
        }
        if ($cid>0) {
            if ($kw) {
                $goods_list = ImsEweiShopGoods::mk()->where($where)->where('title|description|content|goodssn|keywords','like','%'.$kw.'%')->where('pcate|ccate|tcate','=', $cid)->order('sort', 'DESC')->limit($offset,$limit)->select();
            }else{
                $goods_list = ImsEweiShopGoods::mk()->where('pcate|ccate|tcate','=', $cid)->where($where)->order('sort', 'DESC')->limit($offset,$limit)->select();
            }
        }else{
            if ($kw) {
                $goods_list = ImsEweiShopGoods::mk()->where($where)->where('title|description|content|goodssn|keywords','like','%'.$kw.'%')->order('sort', 'DESC')->limit($offset,$limit)->select();
            }else{
                $goods_list = ImsEweiShopGoods::mk()->where($where)->order('sort', 'DESC')->limit($offset,$limit)->select();
            }
        }

        $data = ImsEweiShopGoods::formatDatas($goods_list);

        $this->echojson(0,'商品列表'.ImsEweiShopGoods::getlastsql(), $data);
    }

    public function cate()
    {

        $where  = [
            'enabled' => 1,
            'uniacid' => 2,
            'status' => 1,
        ];

        $cid = Request::param('cid');
        if ($cid > 0) {
            $where['pid'] = $cid;
        }else{
            $where['pid'] = 0;
        }

        $list = ImsEweiShopCategory::mk()->where($where)->order('sort desc,id asc')->limit(0,50)->select();
        $data = ImsEweiShopCategory::formatDatas($list);

        $this->echojson(0,'获取商品分类成功'.ImsEweiShopCategory::getlastsql(), $data);
    }

    public function tjgoods(){
        $this->echojson(0,'获取商品分类成功', []);
    }

    public function details(){
        $id = Request::param('id');

        $data = ImsEweiShopGoods::items(['id'=>$id]);

        $specs = ImsEweiShopGoodsSpec::getSpecsByGoodsId($data['id']);
        $data['specs'] = $specs;
        $this->echojson(0,'获取商品信息成功', $data);
    }

    public function goodsAppraises(){
        $id = Request::param('id');
        $data = [
            "list"=> [],
            "page"=> "1",
            "totalPage"=> 0
        ];
        $this->echojson(0,'商品评价', $data);
    }

}
